function [FY, FX] = featureProgram(Y, X, P)
%Feature program function
%this function receives an raw data X and parameters P
%this function returns the features F
%the matrix X is M examples X n variables
%the vector P = {w1,l1;w2,l2;w3,l3;...;wn,ln}
%the Feature matrix returned F is M samples with N featurs

%number of examples in the data set
m = size(X,1);

%numbers of variables in the data set
n = size(X,2);

%initialize the feature matrix
F = ones(m,n) .* nan;


%calculate the feature for each city in the data set
for i = 1:n
    %get the lag and window size for each variable
    wsize = P(i,1);
    lag = P(i,2);
    
    %calculate the sum inside of the window for each t
    for t = m:-1:1
                
        %auxliar variable to peform the sum up
        k_i = t - lag - wsize + 1;
        k_f = t - lag;
        
        %test if the start of window will be outside the data vector index
        if (k_i <= 0)
            k_i = 1;
        end;
        %test if the whole window will be outside the vector index
        if(k_f <= 0)
            %put zero if the window be outside the vector index
            F(t,i) = nan;
        else
            %execute the sum up over the window
             F(t,i) = sum(X(k_i:k_f,i));
        end;
        
    end;
end;

%remove points where there are not data enough to predict
validData_Ind = (sum(isnan(F),2) == 0);
validData_Ind = and(validData_Ind, ~isnan(Y));

FX = F(validData_Ind,:);
FY = Y(validData_Ind,:);

end








